# Add locations to the $PATH that actually exist.
PATH="/usr/local/bin:$PATH"
[ -d "/usr/texbin" ]                  && PATH="/usr/texbin:$PATH"
[ -d "/usr/local/share/npm/bin" ]     && PATH="/usr/local/share/npm/bin:$PATH"
[ -d "/usr/local/sbin" ]              && PATH="/usr/local/sbin:$PATH"
[ -d "$HOME/.local/bin" ]             && PATH="$PATH:$HOME/.local/bin"
[ -d "$HOME/.composer/vendor/bin" ]   && PATH="$PATH:$HOME/.composer/vendor/bin"
[ -d "$HOME/go/bin" ]                 && PATH="$PATH:$HOME/go/bin"
[ -d "$HOME/Library/Python/2.7/bin" ] && PATH="$PATH:$HOME/Library/Python/2.7/bin"

# Install with sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"
if [ -d "/home/linuxbrew/.linuxbrew/bin/brew" ]; then
  eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
fi

if [ -d "$HOME/Library/Android/sdk" ]; then
  export ANDROID_HOME="$HOME/Library/Android/sdk"
fi

if [ -d "$ANDROID_HOME/tools" ]; then
  PATH="$PATH:${ANDROID_HOME}/tools"
fi

if [ -d "$ANDROID_HOME/platform-tools" ]; then
  PATH="$PATH:${ANDROID_HOME}/platform-tools"
fi

# Add my scripts directory to my path if it's there.
if [ -d "$HOME/bin" ]; then
  PATH="$HOME/bin:$PATH"
fi

# Set up NPM locally-global paths
if [ ! -d "${HOME}/.node_modules" ]; then
  mkdir "${HOME}/.node_modules"
fi
NPM_PACKAGES="${HOME}/.node_modules"
PATH="$NPM_PACKAGES/bin:$PATH"
unset MANPATH
export MANPATH="${NPM_PACKAGES}/share/man:$(manpath)"

# Initialize rbenv if it exists on this system and isn't yet in our path.
# This prevents path duplication in tmux when a sub-shell runs this script
# again.
if [ -d $HOME/.rbenv/bin ] && [[ ! "$PATH" =~ "rbenv" ]]; then
  export PATH="$HOME/.rbenv/bin:$PATH"

  if [ -d "$HOME/.rbenv/plugins/ruby-build/bin" ]; then
    export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"
  fi

  type rbenv >/dev/null 2>&1
  [[ "$?" == 0 ]] && eval "$(rbenv init -)"
fi

# Configure my Go workspace, if I have it.
if [ -d $HOME/go ]; then
  type go >/dev/null 2>&1
  if [[ "$?" == 0 ]]; then
    export GOPATH=$HOME/go
  fi
fi

# Put the SSH agent socket in a predictable location. If we have an
# $SSH_AUTH_SOCK value already, link to it from a known path in /tmp. If we do
# not have an $SSH_AUTH_SOCK value but the socket link exists in /tmp, set the
# value to that link. The first case typically takes place when first
# connecting to a remote host and the second case typically takes place when
# re-attaching a previously detached tmux session.
SOCK="/tmp/ssh-agent-$USER-socket"
if [ $SSH_AUTH_SOCK ] && [ $SSH_AUTH_SOCK != $SOCK ]; then
  rm -f "/tmp/ssh-agent-$USER-socket"
  ln -sf $SSH_AUTH_SOCK $SOCK
  export SSH_AUTH_SOCK=$SOCK
elif [ -h $SOCK ] && [ ! $SSH_AUTH_SOCK ]; then
  export SSH_AUTH_SOCK=$SOCK
fi

## I don't know why this isn't configured by default.
export LD_LIBRARY_PATH=/usr/local/lib

# The next line updates PATH for the Google Cloud SDK.
if [ -f '/Users/airborne/google-cloud-sdk/path.zsh.inc' ]; then
  source '/Users/airborne/google-cloud-sdk/path.zsh.inc';
fi

# The next line enables shell command completion for gcloud.
if [ -f '/Users/airborne/google-cloud-sdk/completion.zsh.inc' ]; then
  source '/Users/airborne/google-cloud-sdk/completion.zsh.inc';
fi

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

# Finally, remove duplicate PATH entries.
typeset -a paths result
paths=($path)

while [[ ${#paths} -gt 0 ]]; do
  p="${paths[1]}"
  shift paths
  [[ -z ${paths[(r)$p]} ]] && result+="$p"
done

clean_path=${(j+:+)result}
export PATH
export PATH=$clean_path
